<?php include_once __DIR__.'/../inc/userhead.html';?>
<?php include_once __DIR__.'/../inc/usermenu.html';?>
<!--导航条-->
<!--右面板-->
<div class="main_panel">
    <!--头部-->
    <div class="panel_head">
        <ul class="title">
            <li><a href="javascript:;">我的购物车</a></li>
        </ul>
    </div>
    <!--头部-->
    <div style="height:25px"></div>
    <!--内容-->
    <div class="panel_list">
        <ul class="lievl clearAfter">
            <li class="w162">商品名称</li>
            <li class="w162">运费</li>
            <li class="w162">单价</li>
            <li class="w162">数量</li>
            <li class="w162">合计</li>
            <li class="w162">操作</li>
        </ul>
        <!--商品列表-->
        <p>
            总邮费: ￥<span id='shop_postage'><?=$postage_total_price?></span>
        </p>
        <!-- <p>
            温馨提示:购物车商品件数超过5件,将多收10元运费
        </p> -->
        <ul id="cart_goods_list" class="buy_list clearAfter">
            <?php if($cart_list):?>
            <?php foreach($cart_list as $k => $v):?>
            <li>
                <div class="buy_title">
                    <input type="checkbox" onclick="change_cart_total_price()" class="selectorCheck" switchbuycar="single" value="<?=$v['id']?>" checked="">

                </div>
                <ul class="buy_date">
                    <li class="w162">
                        <a href="/goods/detail?gid=<?=$v['id']?>"><img class="goodsPic" src="<?=$v['pic']?>"></a>
                        <dl class="goodsType">
                            <dt><a href="/goods/detail?gid=<?=$v['id']?>" class="key"><?=$v['goods_name']?></a></dt>
                            <dd><a href="/goods/detail?gid=<?=$v['id']?>"><?=$v['title']?></a></dd>
                        </dl>
                    </li>
                    <li class="w162">
                        <span class="buy_time"><span id='postage_<?=$v['id']?>' data-num='<?=$v['nums']?>' data-postage='<?=$v['postage']?>'><?=$v['postage']?></span></span>
                    </li>
                    <li class="w162" id="price_<?=$v['id']?>">
                        <?=$v['price']?>
                    </li>
                    <li class="w162">
                        <div class="goodsCount">
                            <a href="javascript:;" onclick="change_cart_goods_nums('<?=$v['id']?>', 'cut')" class="countBtn l">-</a>
                            <input type="text" id="cartNums_<?=$v['id']?>" class="countValue" value="<?=$v['nums']?>">
                            <a href="javascript:" onclick="change_cart_goods_nums('<?=$v['id']?>', 'add');" class="countBtn r">+</a> </div>
                    </li>
                    <li class="w162" singleprice="" id="total_price_<?=$v['id']?>">￥<?=$v['total_price']?></li>
                    <li class="w162"> <a href="javascript:void(0)" onclick="del_cart_goods('<?=$v['cart_id']?>')" class="key">删除商品</a> </li>
                </ul>
            </li>
            <?php endforeach;?>
            <?php else:?>
            <p class="nullPorm">暂无商品，去逛逛其他地方吧！</p>
            <?php endif;?>
        </ul>
        <!--商品列表-->
        <!--结算开始-->
        <div class="buy_control">
            <span class="l"><input type="checkbox" switchbuycar="allCheck" onclick="selectAll()" class="clicked" checked="">全选</span>
            <ul class="r">
                <li>已选商品：<em class="ctrlKey" id="buyCarSum"><?=$cart_count?></em>件</li>
                <li>合计金额：<em class="ctrlKey" id="allMoney">￥<?=$cart_total_price?></em></li>
                <li><a href="javascript:;" onclick="$('#usBuyCarPayDiv').show()" class="payButton">结算</a></li>
            </ul>
        </div>
        <!--结算结束-->
    </div>
    <!--内容-->
</div>
<!--右面板-->
</div>
<!--w1200-->
<div id="pay" class="hide"></div>
<!--充值与出金弹出层结束-->
<script>
    $(function(){
        clearInterval();
    })
    function change_cart_goods_nums(id,type){
        var num = $("#cartNums_"+id).val();
        if(type == "cut"){
            if(num <= 1){
                warn("不能再少了");
            }else{
                num--;
            }
        }else if(type == "add"){
            if(num >= 99){
                warn("购买量不能超过最大库存");
            }else{
                num++;
            }
        }
        $("#cartNums_"+id).val(num);
        var postage = $("#postage_"+id).data("postage");//运费
        var total_postage = postage*num;//总运费
        //修改自己商品价格
        var price = parseFloat($("#price_"+id).html());
        var total_price = price*num+total_postage;//商品总价格
        $("#total_price_"+id).html("￥"+total_price);
        //运费重新赋值
        $("#postage_"+id).html(total_postage);
        $("#postage_"+id).attr("data-num",num);
        $("#postage_"+id).attr("data-postage",total_postage);
        change_cart_total_price();
    }
    //修改购物总数及总价
    function change_cart_total_price(){
        var cart_total_price = 0;
        var cart_count = 0;
        var user_money = <?=$userinfo['money']?>;
        var total_postage = 0; //总运费
        var cart_num_count = 0; //购物车总件数
        //循环选中的购物车商品
        $("#cart_goods_list").find(":checkbox:checked").each(function(index,obj){
            var cart_id = $(obj).val();
            cart_total_price += parseFloat($("#total_price_"+cart_id).html().replace("￥",""));
            cart_num_count += parseInt($("#cartNums_"+cart_id).val());
            total_postage += parseFloat($("#postage_"+cart_id).html());
            cart_count++;
        })
        var postage_limit = <?=5?>;
        var shop_postage = <?=10?>;
        //如果订单商品超出设置，运费叠加上店铺设置运费
        if(cart_num_count>postage_limit){
            total_postage += shop_postage;
            cart_total_price += shop_postage;
        }
        $("#shop_postage").html(total_postage);
        $("#buyCarSum").html(cart_count);
        $("#allMoney").html("￥"+cart_total_price);
        var pay_price = cart_total_price-user_money;
        if(pay_price<0)pay_price=0;
        $("#payId").html(pay_price);
    }
    //反选
    function selectAll(){
        $("#cart_goods_list :checkbox").each(function(){
            var state = $(this).prop('checked');
            $(this).prop('checked',!state);
        });
        change_cart_total_price();
    }
    function del_cart_goods(id){
        layer.confirm('确认要删除购物车商品吗?', function(index){
            $.ajax({
                url:"/user/delCartGoods",
                method:"post",
                dataType:"json",
                data:{id:id},
                success:function(res){
                    my.action(res);
                    // layer.close(index);
                }
            });
        });
    }
    //结算支付购物车选中订单
    function pay_cart_goods(){
        var address_id = $("#address_id :selected").val();
        var goods_ids = {};
        $("#cart_goods_list").find(":checkbox:checked").each(function(i,e){
            var nums = $(e).parent().parent().find(".countValue").val();
            goods_ids[$(e).val()] = nums;
        });
        var payPrice = parseFloat($("#payId").html());
        $.ajax({
            url:"../order/createOrder",
            method:"post",
            dataType:"json",
            data:{goods_price_ids:goods_ids,address_id:address_id,pay_money:payPrice,pay_type:0,goods_type:0},
            success:function(res){
                if(res.code == 0 ){
                    window.open(res.data.qr_code);
                    //轮询订单状态
                    
                }else{
                    my.action(res);
                    // layer.close(index);
                }

            }
        });
    }
    function checkRecharge(order_no,order_goods_no){
        console.log(order_no);
        console.log(order_goods_no);
        return;

    }
    //选择收货地址
    function change_user_address(address_id){
        $.ajax({
            url:"/user/getUserAddress",
            method:"post",
            dataType:"json",
            data:{id:address_id},
            success:function(res){
                $("#sh_name").html(res.data.address_name);
                $("#sh_phone").html(res.data.address_phone);
            }
        });
    }
</script>
<div class="hide" id="usBuyCarPayDiv">
    <div class="Transparent_layer"></div>
    <div class="play">
        <h3>确认支付<i title="关闭" onclick="$('#usBuyCarPayDiv').hide()"></i></h3>
        <ul class="wrap">
            <input type="hidden" id="" value="<?=isset($address[0]['id'])?$address[0]['id']:''; ?>" />
            <li> <span>收货人：</span>
                <p id="sh_name"><?=isset($address[0]['address_name'])?$address[0]['address_name']:''; ?></p>
            </li>
            <li> <span>联系电话：</span>
                <p id="sh_phone"><?=isset($address[0]['address_phone'])?$address[0]['address_phone']:''; ?></p>
            </li>
            <li> <span>详细地址：</span>
                <select onchange="change_user_address(this.value)" id="address_id" name='workFlow' class='order_sel'>
                    <?php foreach($address as $k=>$v):?>
                    <option value="<?=$v['id']?>" ><?=$v['address']?></option>
                    <?php endforeach; ?>
                </select>
                <br/>
                <a href="<?=url('user/userAddr')?>" class="keyRed" style="text-decoration:underline">设置地址>></a>
            </li>
            <li> <span>当前余额：</span>
                <p class="keyRed">￥<?=$userinfo['money']?></p>
            </li>
            <li>
                <span>还需支付：</span>
                <p class="keyRed">
                    ￥<b id="payId"><?=$cart_total_price-$userinfo['money']>0?$cart_total_price-$userinfo['money']:0?></b>
                </p>
            </li>
            <li> <span>支付方式：</span>
                <p class="keyRed">
                    <input type="checkbox"  class="selectorCheck" switchbuycar="single" checked="" value="">微信支付
                </p>
            </li>
            <li style="border:none; padding:0; text-align:center;">
                <a href="javascript:;" class="confrim" onclick="pay_cart_goods()">立即支付</a>
            </li>
        </ul>
    </div>
</div>
<?php include_once __DIR__.'/../inc/foot.html';?>
